//这就是命令的接口
interface Command {
    execute(): void
}
//这就是具体的命令
class OpenCommand implements Command {
    declare private _app: Application
    declare private _response: string
    constructor(app: Application) {
        this._app = app
    }
    askUser(): string {
        return '1'
    }
    execute(): void {
        const name = this.askUser()
        if (name != '') {
            const document = new Document(name)
            this._app.add(document)
            document.open()
        }
    }

}
//这就是接收者
export class Application {
    add(doc: Document) {
        console.log("应用程序添加了文档");
    }
}
//这就是接收者
class Document {
    declare private _name: string
    constructor(name: string) {
        this._name = name
    }
    open() {
        console.log("打开“" + this._name + "”文档");
    }
    paste() {
        console.log("执行粘贴");
    }
}
//这就是具体的命令
class PasteCommand implements Command {
    declare private _document: Document
    constructor(doc: Document) {
        this._document = doc
    }
    execute(): void {
        this._document.paste()
    }

}


class SimpleCommand<Receiver> implements Command {

    declare private _action: (this: Receiver) => void;//命令需要执行的方法
    declare private _receiver: Receiver//执行命令的对象

    constructor(receiver: Receiver, action: (this: Receiver) => void) {
        this._receiver = receiver;
        this._action = action;
    }
    execute(): void {
        this._action.call(this._receiver)
    }

}

const app = new Application()
const doc = new Document('笔记')
const opencmd = new OpenCommand(app)
const openCMD = new SimpleCommand<OpenCommand>(opencmd, opencmd.execute.bind(opencmd))
openCMD.execute()


//多命令模式
class MacroCommand implements Command {
    declare private _cmds: Command[];
    execute(): void {
        for (const cmd of this._cmds) {
            cmd.execute();
        }
    }
    add(command: Command) {
        this._cmds.push(command)
    }
    remove(command: Command) {
        this._cmds = this._cmds.filter(cmd => cmd !== command);
    }

}